﻿using UnityEngine;
using System.Collections;
using Easing;


using XInputDotNetPure; // Required in C#



public class MoveBlock : MonoBehaviour {

	private Vector3 	InitialPosition;
	private Vector3		TargetPosition;
	public float 		MaximumExtention = 5;
	public Vector3		Direction = new Vector3(0, 1, 0);
	private float 		TimeToFullExtention = 1;
	public float		Speed = 3;
	private float 		Timer = 0;
	private float		CurrentExtention = 0;

	public GameObject	CorrespondingCursor;
	PlayerCursor cursorScript;

	bool playerIndexSet = false;
	PlayerIndex playerIndex;
	GamePadState state;
	GamePadState prevState;

	GameObject Top1;
	MeshFilter meshFilter;
    // Use this for initialization
	void Start () 
	{
		Timer = 0;
		TimeToFullExtention = MaximumExtention / Speed;
		InitialPosition = transform.position;
		TargetPosition = InitialPosition + (Direction * MaximumExtention);
		
		FindPlayerIndex();
		cursorScript = (PlayerCursor)CorrespondingCursor.GetComponent(typeof(PlayerCursor));



		/* Attempts at deformation
		Transform first = transform.GetChild(0);
		Transform transy = first.FindChild("top1");

		meshFilter = (MeshFilter)transform.GetComponent(typeof(MeshFilter));
		Vector3[] verts = meshFilter.mesh.vertices;
		
		for(int i = 0; i < meshFilter.mesh.vertexCount/2; i++)
		{
			verts[i] = verts[i] /2.2f;
		}


//		meshFilter.mesh.vertices = verts;
//		meshFilter.mesh.RecalculateBounds();
		meshFilter.mesh.vertices = verts;
		meshFilter.mesh.RecalculateBounds();
		MeshCollider col = (MeshCollider)transform.GetComponent(typeof(MeshCollider));
		col.sharedMesh.vertices = verts;
		col.sharedMesh.RecalculateBounds();

		//Top1.transform.Rotate(new Vector3(0,1,0), 20);
*/
	}
    
	void FindPlayerIndex()
	{
		// Find a PlayerIndex, for a single player game
		// Will find the first controller that is connected ans use it
		if (!playerIndexSet || !prevState.IsConnected)
		{
			for (int i = 0; i < 4; ++i)
			{
				PlayerIndex testPlayerIndex = (PlayerIndex)i;
				GamePadState testState = GamePad.GetState(testPlayerIndex);
				if (testState.IsConnected)
				{
					Debug.Log(string.Format("GamePad found {0}", testPlayerIndex));
					playerIndex = testPlayerIndex;
					playerIndexSet = true;
				}
			}
		}
		
		prevState = state;
		state = GamePad.GetState(playerIndex);
        
    }
    
    // Update is called once per frame
    void Update () 
    {
		prevState = state;
		state = GamePad.GetState(playerIndex);
        
		float intensity = cursorScript.GetIntensityInDirection();
		float targetExtention = MaximumExtention * DistanceFromCursorModifier(); 

		if(intensity == 0)
		{
			targetExtention = targetExtention / 10;
			intensity = 0.5f;
		}	

		float dir = targetExtention - CurrentExtention;
		dir = Mathf.Clamp(dir, -1, 1);

		CurrentExtention = CurrentExtention  + (Speed * Time.deltaTime * dir * intensity);

		rigidbody.MovePosition(InitialPosition + (Direction * CurrentExtention ));

		//MeshFilter filter = ((MeshFilter)Top1.GetComponent(typeof(MeshFilter)));


	}


	float DistanceFromCursorModifier()
	{
		float dist = cursorScript.GetDistanceFromCursor(transform.position);

		float mod = Mathf.Clamp(dist, 1.0f, 4.0f);
		mod = mod - 1.0f;
		mod = mod /3;
		mod = 1 - mod;

		return mod;

	}










}
